<script setup lang="ts">
import { ref, reactive, withDefaults, defineProps } from "vue";
import ReCol from "@/components/ReCol";

import { FormProps } from "./types";
import { transformI18n } from "@/plugins/i18n";
import { IconSelect } from "@/components/ReIcon";
import Segmented from "@/components/ReSegmented";
import ReAnimateSelector from "@/components/ReAnimateSelector";
import type { FormRules } from "element-plus";

const props = withDefaults(defineProps<FormProps>(), {
  formInline: () => ({})
});

const ruleFormRef = ref();
const newFormInline = ref(props.formInline);
console.log("newFormInline", newFormInline.value);

console.log("newFormInline", newFormInline.value);
const formRules = reactive(<FormRules>{
  name: [{ required: true, message: "分类名称为必填项", trigger: "blur" }],
  // pathName: [{ required: true, message: "路由名称为必填项", trigger: "blur" }],
  parentCid: [
    { required: true, message: "上级分类必须选择", trigger: "change" }
  ],
  showStatus: [
    { required: true, message: "请选择否显示分类", trigger: "blur" }
  ],
  productUnit: [
    { required: true, message: "计量单位为必填项", trigger: "blur" }
  ],
  type: [{ required: true, message: "请选择分类类型", trigger: "blur" }]
});

const changecascader = value => {
  console.log(value);
  console.log(newFormInline.value);
};

function getRef() {
  return ruleFormRef.value;
}

defineExpose({ getRef });
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="newFormInline"
    :rules="formRules"
    label-width="82px"
  >
    <el-row :gutter="30">
      <re-col>
        <el-form-item label="分类类型" prop="type">
          <el-radio-group v-model="newFormInline.type">
            <el-radio :label="0">平台</el-radio>
            <el-radio :label="1">店铺</el-radio>
          </el-radio-group>
        </el-form-item>
      </re-col>

      <re-col>
        <el-form-item label="上级分类" prop="parentCid">
          <el-cascader
            v-model="newFormInline.parentCid"
            class="w-full"
            :options="newFormInline.higherMenuOptions"
            :props="{
              value: 'id',
              label: 'name',
              emitPath: false,
              checkStrictly: false
            }"
            clearable
            filterable
            placeholder="请选择上级分类"
            @change="changecascader"
          >
            <template #default="{ node, data }">
              <span>{{ transformI18n(data.name) }}</span>
              <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
            </template>
          </el-cascader>
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="分类名称" prop="name">
          <el-input
            v-model="newFormInline.name"
            clearable
            placeholder="请输入分类名称"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="菜单排序" prop="sort">
          <el-input-number
            v-model="newFormInline.sort"
            class="!w-full"
            :min="1"
            :max="9999"
            controls-position="right"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="分类图标" prop="icon">
          <IconSelect v-model="newFormInline.icon" class="w-full" />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <!-- 按钮级别权限设置 -->
        <el-form-item label="计量单位" prop="productUnit">
          <el-input
            v-model="newFormInline.productUnit"
            clearable
            placeholder="请输入计量单位"
          />
        </el-form-item>
      </re-col>

      <re-col :value="12" :xs="24" :sm="24">
        <el-form-item label="是否显示" prop="showStatus">
          <el-radio-group v-model="newFormInline.showStatus">
            <el-radio :value="0">不显示</el-radio>
            <el-radio :value="1">显示</el-radio>
          </el-radio-group>
        </el-form-item>
      </re-col>
      <el-form-item label="是否启用" prop="enableStatus">
        <el-radio-group v-model="newFormInline.enableStatus">
          <el-radio :value="0">禁用</el-radio>
          <el-radio :value="1">启用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-row>
  </el-form>
</template>
